package com.martinwj.controller.email;

import com.martinwj.entity.Email;
import com.martinwj.entity.Result;
import com.martinwj.exception.SysException;
import com.martinwj.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: EmaiAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */

@Controller
@RequestMapping("email_info")
public class EmaiAction {
    @Autowired
    private EmailService emailService;

    /**
     * 查询所有邮件设置
     */
    @RequestMapping("list.action")
    public String list(ModelMap map) {

        List<Email> list = emailService.list();
        map.put("list", list);

        return "admin/email_info/list";
    }

    /**
     * 保存邮件设置
     * @param idArr 主键数组
     * @param smtpArr SMTP 服务器数组
     * @param portArr 端口数组
     * @param emailArr 发信人邮件地址数组
     * @param passwordArr SMTP 身份验证密码数组
     * @return
     * @throws SysException
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(
            @RequestParam(value="idArr") String[] idArr,
            @RequestParam(value="smtpArr") String[] smtpArr,
            @RequestParam(value="portArr") String[] portArr,
            @RequestParam(value="emailArr") String[] emailArr,
            @RequestParam(value="passwordArr") String[] passwordArr) throws SysException {

        List<Email> emailInfoList = new ArrayList<Email>();

        // 判断是否已有既存数据
        if (idArr.length==0) {
            // 全是新增
            // 遍历smtpArr数组
            for (int i=0; i<smtpArr.length; i++) {
                Email emailInfo = new Email();
                emailInfo.setSmtp(smtpArr[i]);
                emailInfo.setPort(portArr[i]);
                emailInfo.setEmail(emailArr[i]);
                emailInfo.setPassword(passwordArr[i]);

                emailInfoList.add(emailInfo);
            }
        } else {
            // 遍历idArr数组
            for (int i=0; i<idArr.length; i++) {
                Email emailInfo = new Email();
                emailInfo.setId(idArr[i]);
                emailInfo.setSmtp(smtpArr[i]);
                emailInfo.setPort(portArr[i]);
                emailInfo.setEmail(emailArr[i]);
                emailInfo.setPassword(passwordArr[i]);

                emailInfoList.add(emailInfo);
            }
        }

        emailService.save(emailInfoList);

        return Result.success();
    }

    /**
     * 删除
     * @param idArr 邮件设置主键数组
     * @throws SysException
     */
    @RequestMapping("delete.json")
    @ResponseBody
    public Result delete(@RequestParam(value="idArr") String[] idArr) throws SysException {

        emailService.delete(idArr);
        return Result.success();
    }

    /**
     * 检测邮件发送
     * @param id 邮件设置表的主键
     * @param toEmail 测试收件人的邮箱地址
     * @return
     * @throws SysException
     */
    @RequestMapping("test.json")
    @ResponseBody
    public Result test(
            @RequestParam(value="id") String id,
            @RequestParam(value="toEmail") String toEmail) throws SysException {

        emailService.emailTest(id, toEmail);

        return Result.success();
    }
}
